﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Exp.DAL
Imports Exp.Model
Public Class UnitType_service
    Public Function GetAll() As List(Of REM_UnitType)
        Dim ctx As New EXPEntities
        Dim list = ctx.REM_UnitType.ToList()
        Return list
    End Function
    Public Function GetByName(ByVal sUnitTypeName As String) As List(Of REM_UnitType)
        Dim ctx As New EXPEntities
        Dim lstObj As New List(Of REM_UnitType)
        lstObj = ctx.REM_UnitType.Where(Function(o) o.UnitTypeName = sUnitTypeName).ToList()
        Return lstObj
    End Function
    Public Function GetByID(ByVal ID As Integer) As REM_UnitType
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_UnitType.Where(Function(o) o.UnitType_ID = ID).FirstOrDefault()
        Return obj
    End Function
    Public Sub Add(objREM_UnitType As REM_UnitType)
        Dim ctx As New EXPEntities
        ctx.REM_UnitType.AddObject(objREM_UnitType)
        ctx.SaveChanges()
    End Sub
    Public Sub Delete(ByVal iUnitTypeID As Integer)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_UnitType.Where(Function(o) o.UnitType_ID = iUnitTypeID).FirstOrDefault()
        ctx.REM_UnitType.DeleteObject(obj)
        ctx.SaveChanges()
    End Sub
    Public Sub Edit(objREM_UnitType As REM_UnitType)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_UnitType.Where(Function(o) o.UnitType_ID = objREM_UnitType.UnitType_ID).FirstOrDefault()
        obj.UnitTypeName = objREM_UnitType.UnitTypeName
        obj.UpdateDate = objREM_UnitType.UpdateDate
        obj.UpdateUser = objREM_UnitType.UpdateUser
        'obj.CreateUser = objREM_UnitType.CreateUser
        'obj.CreateDate = objREM_UnitType.CreateDate
        ctx.SaveChanges()
    End Sub
    Public Function GetLastID() As Integer
        Dim ctx As New EXPEntities
        Dim iReturn As Integer = 0
        Dim lstObj As New List(Of REM_UnitType)
        lstObj = ctx.REM_UnitType.ToList()
        lstObj = ctx.REM_UnitType.OrderByDescending(Function(p) p.UnitType_ID).ToList()
        If Not lstObj Is Nothing Then
            If lstObj.Count > 0 Then
                iReturn = lstObj.Item(0).UnitType_ID + 1
            Else
                iReturn = 1
            End If
        End If
            Return iReturn
    End Function
End Class
